/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: avp_packet.h
 *
 * Purpose: define packet structure for av player
 *
 * Developer:
 *   wen.gu , 2022-01-19
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __AVP_PACKET_H__
#define __AVP_PACKET_H__

#include <stdio.h>
#include "avp_type.h"
#include "avp_error.h"
//#include "avp_media.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START


struct _avp_packet;
typedef struct _avp_packet avp_packet_t;

typedef void (*avp_packet_release_func)(avp_packet_t* pkt);

struct _avp_packet
{
    GBOL is_eof; /** GTRUE: this is a empty packet, just mark current stream is eof. GFALSE: not eof */
    GS32 stream_idx;
    GU32 duration; /** frame continue duration(uint in millisecond(ms), if 0, then invalid */    
    GU32 offset;
    GU32 size;
    GU32 capacity;
    GBUF buf;
    GS64 pts; /** Presentation timestamp(uint in millisecond(ms) ) */
    GS64 dts; /** Decoding timestamp(uint in millisecond(ms)) */     
    GPTR opaque; /** for some private data of the user  */ 
    avp_packet_release_func release;  /** if null do nothing */
};

struct _avp_packet_queue;
typedef struct _avp_packet_queue* avp_packet_queue_t;


/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
G_API avp_packet_t* avp_packet_create(GU32 size);
G_API avp_packet_t* avp_packet_create2(GBUF buf, GU32 size, avp_packet_release_func on_release, GPTR opaque);

G_API void avp_packet_destroy(avp_packet_t* pkt);


G_API GErrc avp_packet_queue_create(avp_packet_queue_t* pkt_queue, GU32 queue_size);  /** if queue_size == 0, then unlimit the size of queue */
G_API void avp_packet_queue_destory(avp_packet_queue_t pkt_queue);

G_API GErrc avp_packet_queue_put(avp_packet_queue_t pkt_queue, avp_packet_t* pkt);
G_API GErrc avp_packet_queue_get(avp_packet_queue_t pkt_queue, avp_packet_t** pkt, GBOL is_bloack);

G_API GErrc avp_packet_queue_clear(avp_packet_queue_t pkt_queue);

G_API GBOL avp_packet_queue_is_full(avp_packet_queue_t pkt_queue);


G_API GU32 avp_packet_queue_free_size(avp_packet_queue_t pkt_queue);
G_API GU32 avp_packet_queue_remaining_size(avp_packet_queue_t pkt_queue);

/**
 * @brief abort queue wait for avp_packet_queue_get()
 * 
 * @param pkt_queue [in] the packet queue instance
 * @return GErrc success: G_OK, else error code: @see GErrc
 */
G_API GErrc avp_packet_queue_abort(avp_packet_queue_t pkt_queue);



G_C_HEADER_END

#endif /** !__AVP_PACKET_H__ */

